﻿using eDoc.DAL;
using eDocWS.Objects;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace eDocWS
{
    /// <summary>
    /// Summary description for reports
    /// </summary>
    public class reports : IHttpHandler
    {
        private int day = 30;
        private string email = "";
        private string uuid = "";
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["type"] != null && context.Request["type"].ToString() == "logsync")
            {
                if(context.Request["email"] != null && context.Request["email"].ToString() != "")
                {
                    email = context.Request["email"].ToString();
                }
                if(context.Request["uuid"] != null && context.Request["uuid"].ToString() != "")
                {
                    uuid = context.Request["uuid"].ToString();
                }

                if(context.Request["day"] != null && context.Request["day"].ToString() != "")
                {
                    day = Convert.ToInt32(context.Request["day"].ToString());
                }

                string value = GetLogSync(day, email, uuid);
                context.Response.ContentType = "application/json";
                context.Response.Write(value);
                
                
            }
            else
            {
                string value = Error("Thieu param type nen khong biet duoc su dung phuong thuc nao ca.","Khong tim thay kieu report");
                context.Response.ContentType = "application/json";
                context.Response.Write(value);
            }
        }

        private string Error(string message, string title)
        {
            string value = "";
            Error error = new Error();
            error.Title = title;
            error.Message = message;
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            value = javaScriptSerializer.Serialize(error);
            return value;
        }

        private string GetLogSync(int Day, string Email, string Uuid)
        {
            string value = "";
            try
            {
                DataTable dt = new DataTable();
                using (MainDB db = new MainDB())
                {
                    dt = db.StoredProcedures.VNA_Report_LogSync(Day, Email, Uuid);
                    db.Close();
                    db.Dispose();
                }

                DeviceInfo deviceInfo = new DeviceInfo();
                
                if (dt != null && dt.Rows.Count > 0)
                {
                    List<LogSync> logSyncList = new List<LogSync>();
                    deviceInfo.Email = email;
                    deviceInfo.Uuid = Uuid;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        LogSync logSync = new LogSync();
                        
                        logSync.Content = dt.Rows[i]["Content"].ToString();
                        logSync.ErrorCode = dt.Rows[i]["ErrorCode"].ToString();
                        logSync.DateSync = Convert.ToDateTime(dt.Rows[i]["LogDate"]).ToString("dd/MM/yyyy HH:mm");
                        logSync.Actived = Convert.ToBoolean(dt.Rows[i]["Active"]);
                        logSync.Email = dt.Rows[i]["Email"].ToString();
                        logSync.Uuid = dt.Rows[i]["Uuid"].ToString();
                        logSyncList.Add(logSync);
                    }

                    deviceInfo.LogSync = logSyncList;
                }

                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                value = javaScriptSerializer.Serialize(deviceInfo);
            }
            catch(Exception ex)
            {
                value = Error(ex.Message + "/n" + ex.StackTrace, ex.Message);
            }
            return value;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}